//
//  StatisticsLayer.m
//
//  Source file for the class where all aspects of the graphic user interface
//  of the statistics is implemented

#import "StatisticsLayer.h"
#import "MainMenuLayer.h"
#import "GameState.h"
#import "SimpleAudioEngine.h"
#import "GraphLayer.h"

static const int kStatisticsTitleSize = 104;
static const int kTextSize = 40;

@implementation StatisticsLayer

// returns a scene object containing the current layer
// this essentially loads the "screen" for a
// particular section or part of the game
+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
    StatisticsLayer *layer = [StatisticsLayer node];
    
	[scene addChild: layer];
	
	return scene;
}

// standard iOS popup that functions as a stub
// for some of the buttons that haven't been
// implemented yet
-(void) popup
{
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Hello There!"];
    [dialog setMessage:@"You clicked one of the buttons!"];
    [dialog addButtonWithTitle:@"Yes"];
    [dialog addButtonWithTitle:@"No"];
    [dialog show];
    [dialog release];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) graphButtonPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[GraphLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) mainMenuPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[MainMenuLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(id) init
{
    if ((self = [super init])) {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        CCSprite *bg = [CCSprite spriteWithFile:@"menuBackground.png"];
        bg.position = ccp(winSize.width / 2, winSize.height / 2);
        [self addChild:bg z:0];
        
        CCLabelTTF *statisticsTitleLbl = [CCLabelTTF labelWithString:@"Statistics" fontName:@"Marker Felt" fontSize:kStatisticsTitleSize];
        statisticsTitleLbl.position = ccp(winSize.width / 2, winSize.height - kStatisticsTitleSize);
        statisticsTitleLbl.color = ccc3(255, 255, 255);
        [self addChild:statisticsTitleLbl];
        
        //formating for the total time label
        //calculates the total time played in minutes and seconds
        int minutes = floor([[[GameState sharedGameState] getState:@"totalTimePlayed"] integerValue]/60);
        int seconds = floor([[[GameState sharedGameState] getState:@"totalTimePlayed"] integerValue]%60);
        
        
       //format is "minutes:seconds"
        if (seconds > 10){
            
            CCLabelTTF *totaltimeLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Total Time Played: %d:%d",minutes, seconds] fontName:@"Marker Felt" fontSize:kTextSize];
            totaltimeLbl.position = ccp(winSize.width / 2, winSize.height - 250);
            totaltimeLbl.color = ccc3(255, 255, 255);
            [self addChild:totaltimeLbl];
            
            
        }
        
        else {
            //if there is less than two digits of seconds, add 0 after "minutes:"
            CCLabelTTF *totaltimeLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Total Time Played: %d:%d%d",minutes, 0, seconds] fontName:@"Marker Felt" fontSize:kTextSize];
            totaltimeLbl.position = ccp(winSize.width / 2, winSize.height - 250);
            totaltimeLbl.color = ccc3(255, 255, 255);
            [self addChild:totaltimeLbl];
            
        }
        
        
        //total score of all the games you have played
        CCLabelTTF *totalscoreLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Total Score: %d",[[[GameState sharedGameState] getState:@"totalScore"] integerValue]] fontName:@"Marker Felt" fontSize:kTextSize];
        totalscoreLbl.position = ccp(winSize.width / 2, winSize.height - 300);
        totalscoreLbl.color = ccc3(255, 255, 255);
        [self addChild:totalscoreLbl];
        
        //the total amount of games you have played
        CCLabelTTF *totalgamesLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Total Games Played: %d",[[[GameState sharedGameState] getState:@"totalGamesPlayed"] integerValue]] fontName:@"Marker Felt" fontSize:kTextSize];
        totalgamesLbl.position = ccp(winSize.width / 2, winSize.height - 350);
        totalgamesLbl.color = ccc3(255, 255, 255);
        [self addChild:totalgamesLbl];
        
        //average is totalScore/totalGamesPlayed
        float average;
        
        //to catch the condition where no games are played, division by 0 case
        if([[[GameState sharedGameState] getState:@"totalGamesPlayed"]integerValue] != 0){
            
           average = ([[[GameState sharedGameState] getState:@"totalScore"] integerValue]/[[[GameState sharedGameState] getState:@"totalGamesPlayed"] integerValue]);
        }
        else {

            average = 0;
        }
        
        //the average will be displayed to two decimal places
        CCLabelTTF *averagescoreLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Overall Average Score: %.2f",average] fontName:@"Marker Felt" fontSize:kTextSize];
        averagescoreLbl.position = ccp(winSize.width / 2, winSize.height - 400);
        averagescoreLbl.color = ccc3(255, 255, 255);
        [self addChild:averagescoreLbl];
        
        //best time for level 1
        CCLabelTTF *level1bestLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 1 Best Time: %d",[[[GameState sharedGameState] getState:@"level1BestTime"] integerValue]] fontName:@"Marker Felt" fontSize:kTextSize];
        level1bestLbl.position = ccp(winSize.width / 2, winSize.height - 450);
        level1bestLbl.color = ccc3(255, 255, 255);
        [self addChild:level1bestLbl];
        
        //best time for level 2
        CCLabelTTF *level2bestLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 2 Best Time: %d",[[[GameState sharedGameState] getState:@"level2BestTime"] integerValue]] fontName:@"Marker Felt" fontSize:kTextSize];
        level2bestLbl.position = ccp(winSize.width / 2, winSize.height - 500);
        level2bestLbl.color = ccc3(255, 255, 255);
        [self addChild:level2bestLbl];
        
        //best time for level 3
        CCLabelTTF *level3bestLbl = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 3 Best Time: %d",[[[GameState sharedGameState] getState:@"level3BestTime"] integerValue]] fontName:@"Marker Felt" fontSize:kTextSize];
        level3bestLbl.position = ccp(winSize.width / 2, winSize.height - 550);
        level3bestLbl.color = ccc3(255, 255, 255);
        [self addChild:level3bestLbl];
        
        CCMenuItemImage *menuBtn = [CCMenuItemImage itemWithNormalImage:@"mainMenuButton.png"
                                                          selectedImage:@"mainMenuButtonHighlight.png" target:self selector:@selector(mainMenuPressed)];
        
        CCMenuItemImage *graphBtn = [CCMenuItemImage itemWithNormalImage:@"graphButton.png"
                                                          selectedImage:@"graphButtonHighlight.png" target:self selector:@selector(graphButtonPressed)];
        
        CCMenu *menu = [CCMenu menuWithItems:graphBtn, menuBtn, nil];
        [menu alignItemsVerticallyWithPadding:10.0];
        menu.position = ccp(winSize.width / 2, graphBtn.contentSize.height / 2 + 40);
        [self addChild:menu];
        
    }
    
    return self;
}

@end
